import QtQuick 2.8
import QtQuick.Window 2.2
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.3
import QtWeChat 1.0

Page {
    id: root

    property WeChatLogin login

    padding: 0
    background: Item {}

    RowLayout {
        anchors.fill: parent
        spacing: 0

        MenuBar {
            id: menuBar
            Layout.fillHeight: true
            menuAvatar: login.loginAvatar
            onMenuOptionChanged: {
                mainStack.transfer(menuBar.menuOption);
            }
        }

        StackView {
            id: mainStack
            Layout.fillWidth: true
            Layout.fillHeight: true
            initialItem: ChatRoomPage {
                model: login.roomModel
            }
            function transfer(page) {
                if(page == "chat") {
                    mainStack.pop(null)
                }else if(page == "contact") {
                    if(mainStack.depth > 1) {
                        mainStack.replace("qrc:/ContactPage.qml", {"model": login.contactModel});
                    }else{
                        mainStack.push("qrc:/ContactPage.qml", {"model": login.contactModel});
                    }
                }
            }
            popEnter: Transition {
                PropertyAnimation { property: "opacity"; from: 0; to: 1; duration: 300 }
            }
            popExit: Transition {
                PropertyAnimation { property: "opacity"; from: 1; to: 0; duration: 300 }
            }
            pushEnter: Transition {
                PropertyAnimation { property: "opacity"; from: 0; to: 1; duration: 300 }
            }
            pushExit: Transition {
                PropertyAnimation { property: "opacity"; from: 1; to: 0; duration: 300 }
            }
            replaceEnter: Transition {
                PropertyAnimation { property: "opacity"; from: 0; to: 1; duration: 300 }
            }
            replaceExit: Transition {
                PropertyAnimation { property: "opacity"; from: 1; to: 0; duration: 300 }
            }
        }
    }

    Component.onCompleted: {
        var width = 860;
        var height = 640;
        ApplicationWindow.window.visible = false;
        ApplicationWindow.window.minimumWidth = width;
        ApplicationWindow.window.minimumHeight = height;
        ApplicationWindow.window.x = (Screen.desktopAvailableWidth - width) / 2;
        ApplicationWindow.window.y = (Screen.desktopAvailableHeight - height) / 2;
        ApplicationWindow.window.visible = true;
    }
}
